package com.example.demo.service.impl;

import com.example.demo.entity.ArticleImg;
import com.example.demo.mapper.ArticleImgMapper;
import com.example.demo.service.IArticleImgService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 文章图片表 服务实现类
 * </p>
 *
 * @author Mr.Yang
 * @since 2024-06-03
 */
@Service
public class ArticleImgServiceImpl extends ServiceImpl<ArticleImgMapper, ArticleImg> implements IArticleImgService {

    @Autowired
    private ArticleImgMapper articleImgMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addArticleImg(List<ArticleImg> articleImg) {
        try {
            for (ArticleImg img : articleImg) {
                articleImgMapper.insertArticleImg(img);
            }
            // 如果前面的插入都没有问题，返回true
            return true;
        } catch (Exception e) {
            return false; // 发生异常时直接返回false
        }
    }

    @Override
    public List<ArticleImg> selectAllImg() {
        return articleImgMapper.selectAllImg();
    }

    @Override
    public List<ArticleImg> selectByArticleId(Integer articleId) {
        return articleImgMapper.selectByArticleId(articleId);
    }
}
